package com.tool.utils.utils;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.fuyousf.android.fuious.service.PrintInterface;
import com.nld.cloudpos.aidl.AidlDeviceService;
import com.nld.cloudpos.aidl.system.AidlSystem;

/**
 * Created by Administrator on 2017/11/10 0010.
 */

public class FyAidlUtils {

    private static final String TAG = "AidlUtils";

    private static FyAidlUtils fyAidlUtils;

    private static Context mContext;

    private static PrintInterface printService = null;
    private static PrintReceiver printReceiver ;


    private FyAidlUtils(Context context){
        this.mContext = context;
        initPrintService();

    }

    public synchronized static FyAidlUtils getInstance(Context context) {
        if (fyAidlUtils == null) {
            fyAidlUtils = new FyAidlUtils(context);
        }
        return fyAidlUtils;
    }

    public static void unBindService(){
        if (printReceiver != null){
            mContext.unregisterReceiver(printReceiver);
        }
        if( fyAidlUtils != null){
            fyAidlUtils = null;
        }
        mContext.unbindService(printServiceConnection);

//        ToastUtils.showShort(mContext, "解绑成功");
    }



    public static PrintInterface getPrintService(){
        return printService;
    }


    /**
     * 服务连接
     */
    private void initPrintService(){
        printRegisterReceiver();
        Intent printIntent = new Intent(/*"com.fuyousf.android.fuious.service.PrintInterface"*/);
        printIntent.setAction("com.fuyousf.android.fuious.service.PrintInterface");
        printIntent.setPackage("com.fuyousf.android.fuious");
        mContext.bindService(printIntent, printServiceConnection, Context.BIND_AUTO_CREATE);
    }
    private static ServiceConnection printServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            printService = PrintInterface.Stub.asInterface(arg1);
//            ToastUtils.showShort(mContext, "打印服务连接成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            printService = null;
        }

    };

    private void printRegisterReceiver(){
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.fuyousf.android.fuious.service.print");
        printReceiver = new PrintReceiver();
        mContext.registerReceiver(printReceiver, intentFilter);
    }

    class PrintReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String result = intent.getStringExtra("result");
//            etBack.setText("reason："+result);
            if (result.contains("PRINT ERR-")) {
                ToastUtils.showShort(context, result);
            }
        }
    }
}
